﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lephone.Data.SqlEntry;
using Lephone.Data;
using CsharpTpl.common;
using CsharpTpl.model;

namespace CsharpTpl.window
{
    public partial class FrmRoleManage : Form
    {
        private long JSID;
        public FrmRoleManage()
        {
            InitializeComponent();
            roleListView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            roleListView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            roleListView.EditMode = DataGridViewEditMode.EditProgrammatically;
            roleListView.AllowUserToAddRows = false;
            this.Width = 405;
            btnAddRole.Click += new EventHandler(btnAddRole_Click);
            btnQX.Click += new EventHandler(btnQX_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            roleListView.SelectionChanged += new EventHandler(roleListView_SelectionChanged);
            btnModify.Click += new EventHandler(btnAddRole_Click);
            btnDel.Click += new EventHandler(btnDel_Click);
            btnQXFP.Click += new EventHandler(btnQXFP_Click);
            LoadJSList();
        }

        void btnQXFP_Click(object sender, EventArgs e)
        {
            FrmRoleTree frmRoleTree = new FrmRoleTree(JSID);
            //frmRoleTree.Parent = this;
            frmRoleTree.Show(this);
        }

        void btnDel_Click(object sender, EventArgs e)
        {
            if (roleListView.SelectedRows.Count>0)
            {
                if (MessageBox.Show(this, "确认要删除此项记录么?此操作无法恢复.", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes )
                {
                    DbEntry.Delete(T_JS.FindById(JSID));
                    LoadJSList();
                }
            }
        }

        void roleListView_SelectionChanged(object sender, EventArgs e)
        {
            if (roleListView.SelectedRows.Count>0)
            {
                JSID = long.Parse(roleListView.SelectedRows[0].Cells[0].Value.ToString());
                btnModify.Enabled = true;
                btnDel.Enabled = true;
            } 
            else
            {
                btnModify.Enabled = false;
                btnDel.Enabled = false;
            }
        }
        void LoadJSList()
        {
            SqlStatement sql = new SqlStatement("select ID 角色ID,MC 角色名称,BZSM 说明 from T_JS");
            DataSet ds = DbEntry.Context.ExecuteDataset(sql);
            roleListView.DataSource = ds.Tables[0];
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            if (Utils.NotNull(tboxMC,labelForMC))
            {
                if (btnSave.Text=="保存")
                {
                    T_JS js = T_JS.FindOne(CK.K["MC"] == tboxMC.Text.Trim());
                    if (js != null)
                    {
                        MessageBox.Show("该角色名字已经存在");
                        tboxMC.Focus();
                        return;
                    }
                    else
                    {
                        js.MC = tboxMC.Text.Trim();
                        js.BZSM = tboxBZSM.Text.Trim();
                        js.Save();
                    }
                } 
                else
                {
                    T_JS js = T_JS.New;
                    js.Id = JSID;
                    js.MC = tboxMC.Text.Trim();
                    js.BZSM = tboxBZSM.Text.Trim();
                    js.Save();
                }
                LoadJSList();
                btnQX.PerformClick();
            }
            
        }

        void btnQX_Click(object sender, EventArgs e)
        {
            this.Width = 405;
            roleListView.Enabled = true;
        }

        void btnAddRole_Click(object sender, EventArgs e)
        {
            if (this.Width < 500)
            {
                this.Width = 629;
            }
            ToolStripButton btn = (ToolStripButton)sender;
            if (btn.Text.Equals("新增角色"))
            {
                formBox.Text = "新增角色";
                tboxMC.Clear();
                tboxBZSM.Clear();
                btnSave.Text = "保存";
            }
            else
            {
                formBox.Text = "修改角色";
                btnSave.Text = "更新角色";
                tboxMC.Text = roleListView.SelectedRows[0].Cells[1].Value.ToString();
                tboxBZSM.Text = roleListView.SelectedRows[0].Cells[2].Value.ToString();
                roleListView.Enabled = false;
            }
            tboxMC.Focus();
        }
    }
}
